var utilMd5 = require('md5.js');
var api = require('api.js');
var log = require('logs.js');
var requestHandler = {
  params: {},
  success: function (res) {
    // success
  },
  fail: function () {
    // fail
  },
}

//GET请求
function GET(url, requestHandler) {
  request(url, 'GET', requestHandler)
}
//POST请求
function POST_Ticket(url, requestHandler) {
  
  request_ticket(url, 'POST', requestHandler)
}
//POST请求
function POST(url, requestHandler) {
  
    request(url, 'POST', requestHandler)
  }
//POST请求
function POST_Json(url, requestHandler) {
  
  request_json(url, 'POST', requestHandler)
}
//博饼POST请求
function POST_Bobing(url, requestHandler) {
  
    request_bobing(url, 'POST', requestHandler)
}
//兑换券核销
function POST_Exchange(url, requestHandler) {
  
    request_exchange(url, 'POST', requestHandler)
}
function Splicing(data) {
  var theData = '', num = 0;
  for (var key in data) {
    theData = theData + key + '=' + data[key] + '&'
  };
  return theData;
}
function request_json(url, method, requestHandler) {
  //注意：可以对params加密等处理
  log.info('[请求'+url.split("/")[2]+'接口]')
  log.info(Splicing(requestHandler.data))
  wx.request({
    url: getApp().globalData.requestUrl + url,
    data: requestHandler.data,
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    scriptCharset: 'utf-8',
    header: {
      'content-type': 'application/json;charset=utf-8',
    }, // 设置请求的 header
    success: function (res) {
      //注意：可以对参数解密等处理
      requestHandler.success(res)
    },
    fail: function () {
      requestHandler.fail()
    },
    complete: function () {
      // complete
      wx.hideLoading() // 手动关闭加载等待
    }
  })
}
function request(url, method, requestHandler) {
  //注意：可以对params加密等处理
  log.info('[请求'+url.split("/")[2]+'接口]')
  log.info(Splicing(requestHandler.data))
  console.log(getApp().globalData.requestUrl + url + '?' + Splicing(requestHandler.data))
  wx.request({
    url: getApp().globalData.requestUrl + url + '?' + Splicing(requestHandler.data),
    data: [],
    method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    scriptCharset: 'utf-8',
    header: {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
    }, // 设置请求的 header
    success: function (res) {
      //注意：可以对参数解密等处理
      requestHandler.success(res)
    },
    fail: function () {
      requestHandler.fail()
    },
    complete: function () {
      // complete
      wx.hideLoading() // 手动关闭加载等待
    }
  })
}
function request_ticket(url, method, requestHandler) {
    //注意：可以对params加密等处理
    log.info('[请求'+url+'接口]')
    log.info(Splicing(requestHandler.data))
    // console.log(requestHandler.data)
    // console.log(getApp().globalData.requestTicketUrl + url + '?' + Splicing(requestHandler.data))
    wx.request({
        url: getApp().globalData.requestTicketUrl + url,
        data: requestHandler.data,
        method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        scriptCharset: 'utf-8',
        header: {
          'content-type': 'application/json;charset=utf-8',
        }, // 设置请求的 header
      success: function (res) {
        //注意：可以对参数解密等处理
        requestHandler.success(res)
      },
      fail: function (res) {
          log.info(res)
        requestHandler.fail()
      },
      complete: function () {
        // complete
        wx.hideLoading() // 手动关闭加载等待
      }
    })
  }
  function request_bobing(url, method, requestHandler) {
     //注意：可以对params加密等处理
    log.info('[请求'+url+'接口]');
    log.info(Splicing(requestHandler.data))
    wx.request({
        url: getApp().globalData.bobingRequestUrl + url,
        data: requestHandler.data,
        method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        scriptCharset: 'utf-8',
        header: {
        'content-type': 'application/json;charset=utf-8',
        }, // 设置请求的 header
        success: function (res) {
        //注意：可以对参数解密等处理
            requestHandler.success(res)
        },
        fail: function (res) {
            requestHandler.fail()
        },
        complete: function () {
            // complete
            wx.hideLoading() // 手动关闭加载等待
        }
    })
  }
  function request_exchange(url, method, requestHandler) {
    //注意：可以对params加密等处理
    log.info('[请求'+url.split("/")[2]+'接口]')
    log.info(Splicing(requestHandler.data))
    wx.request({
      url: getApp().globalData.hx_url + url,
      data: requestHandler.data,
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      scriptCharset: 'utf-8',
      header: {
        'content-type': 'application/json;charset=utf-8',
      }, // 设置请求的 header
      success: function (res) {
        //注意：可以对参数解密等处理
        requestHandler.success(res)
      },
      fail: function () {
        requestHandler.fail()
      },
      complete: function () {
        // complete
        wx.hideLoading() // 手动关闭加载等待
      }
    })
  }
module.exports = {
  GET: GET,
  POST: POST,
  POST_Ticket: POST_Ticket,
  POST_Json:POST_Json,
  POST_Bobing:POST_Bobing,
  POST_Exchange:POST_Exchange
}